<?php



namespace App\Model;

use App\Util\ImUtil;
use Illuminate\Database\Eloquent\Model;

/**
 * 消息记录
 */
class Message extends Model
{
    //透传消息类型
    const SILENT_MSG_TYPE = [
        //金币不足剩余N分钟通话
        'LACK_COIN' => 'msg_lack_coin',
        //金币消耗完
        'NO_COIN' => 'msg_no_coin',
        //切换女用户（新手福利）
        'CHANGE_FOR_NEW' => 'msg_change_host_for_new',
        //切换女用户（一键语聊）
        'CHANGE_FOR_QUICK' => 'msg_change_host_for_quick',
        //挂断电话
        'HANGUP_CALL' => 'msg_hangup_call',
        //充值礼包'
        'RECHARGE_TIP' => 'msg_pop_recharge_tip',
        //新消息提醒
        'NEW_NOTICE' => 'msg_new_notice',
    ];

    //通知类型 - 系统通知
    const PUSH_TYPE_SYSTEM = 'SYSTEM';
    //通知类型 - 官方通知
    const PUSH_TYPE_OFFICIAL = 'OFFICIAL';
    //自定义系统消息
    const MSG_TYPE_SYSMSG = 'VC:SYS_MSG';

    //纯推送消息
    const MSG_TYPE_PUSH = 'VC:PUSH_MSG';

    const MSG_TYPE_PIC = 'PIC';
    const MSG_TYPE_PIC_TEXT = 'PIC_TEXT';
    const MSG_TYPE_TEXT = 'TEXT';
    const MSG_TYPE_TEXT_LINK = 'TEXT_LINK';
    const MSG_TYPE = [
        self::MSG_TYPE_PIC => '纯图带跳转消息',
        self::MSG_TYPE_PIC_TEXT => '图文带跳转消息',
        self::MSG_TYPE_TEXT => '纯文字消息（有标题）',
        self::MSG_TYPE_TEXT_LINK => '纯文字带跳转消息（有标题）',
    ];

    const MSG_CATE_SYS = 1;
    const MSG_CATE_VRFY = 2;
    const MSG_CATE_WECHAT_APPLY = 3;
    const MSG_CATE = [
        self::MSG_CATE_SYS => '系统消息',
        self::MSG_CATE_VRFY => '审核通知',
        self::MSG_CATE_WECHAT_APPLY => '微信申请',
    ];

    /**
     * 发送透传消息
     * @param $pushUserId
     * @param array $msgInfo
     * @param array $sender
     */
    static function sendSlient($userIds = [], $msgType = '', $extInfo = [])
    {
        if (empty($userIds)) return false;
        $msgInfo =  ['msg_obj' => $extInfo, 'msg_type' => self::SILENT_MSG_TYPE[$msgType]];
        //兼容非数组
        if (!is_array($userIds)) $userIds = [$userIds];
        //获取融云账号
        $accounts = array_values(User::getImAccounts($userIds));
        //发送透传
        return ImUtil::sendSilentMsg($accounts, $msgInfo);
    }

    /**
     * 发送Push推送
     * @param $pushUserId
     * @param array $msgInfo
     * @param array $sender
     */
    static function sendPush($userIds = [], $pushTitle = '', $extInfo = [])
    {
        if (empty($userIds)) return false;
        //兼容非数组
        if (!is_array($userIds)) $userIds = [$userIds];
        //获取融云账号
        $accounts = array_values(User::getImAccounts($userIds));

        //推送消息
        $pushContent = $pushTitle ?: '你有一条新消息';
        //推送内容
        $content = $extInfo;
        //发送透传
        return ImUtil::sendPushMsg(
            $accounts,
            $content,
            self::MSG_TYPE_PUSH,
            $pushContent
        );
    }

    /**
     * 系统消息-纯文字
     */
    static function pushSystem($userId = [], $text = '', $title = '审核通知')
    {
        // if (empty($userIds) || !$text) return false;
        // //兼容非数组
        // if (!is_array($userIds)) $userIds = [$userIds];
        // //获取融云账号
        // $accounts = User::getImAccounts($userIds);
        // return ImUtil::systemPushToSingle($accounts, $text, ImUtil::RONG_TEXT_TYPE, $text);
        return self::pushOfficial($userId, [
            'title' => $title,
            'text' => $text,
            // 'jump_info' => null,
        ], self::MSG_TYPE_TEXT);
    }

    /**
     * 官方通知消息
     */
    static function pushOfficial($userIds = [], $msgInfo = [], $msgType = 'TEXT', $toAll = false)
    {
        if ((empty($userIds) && !$toAll) || !$msgInfo || !$msgType) return false;
        $msgContent = [
            // 'msg_catogory' => self::MSG_CATE_VRFY,
            'msg_type' => $msgType,
            'title' => $msgInfo['title'],
            'text' => $msgInfo['text'],
            'pic' => $msgInfo['pic'] ?? '',
            'jump_info' => !isset($msgInfo['jump_info']) ? null : [
                'jump_type' => $msgInfo['jump_info']['jump_type'] ?? 'h5',
                'jump_text' => $msgInfo['jump_info']['jump_text'] ?? '查看详情',
                'jump_link' => $msgInfo['jump_info']['jump_link'] ?? '',
            ]
        ];
        $accounts = User::getImAccounts($userIds, false);
        if ($toAll) {
            $ret = ImUtil::officialPushToAll($msgContent, self::MSG_TYPE_SYSMSG, '你有一条新的官方消息~');
        } else {
            $ret = ImUtil::officialPushToSingle($accounts, $msgContent, self::MSG_TYPE_SYSMSG, '你有一条新的官方消息~');
        }
        return $ret;
    }

    /************* 测试 *************/
    static function testPush($userId = 0)
    {
        $msgContent = [
            'title' => '测试标题',
            'text' => '测试内容测试内容测试内容测试内容测试内容测试内容测试内容',
            'pic' => 'http://img.cdn.nightvoice.cn/images/a0272cf8340365f09516db28bfe0b6d0.jfif?x-oss-process=image/resize,m_fill,h_100,w_200',
            'jump_info' => [
                'jump_type' => 'h5',
                'jump_link' => 'https://www.bilibili.com/'
            ]
        ];
        return Message::pushOfficial([$userId], $msgContent, PushMessage::MSG_TYPE_PIC_TEXT);
    }
}
